<?php

namespace app\controllers;

use Yii;
use app\models\App;
use app\models\AppSearch;
use app\models\MarketCategory;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\Sort;
// use yii\data\ArrayDataProvider;
// use yii\data\Pagination;

/**
 * AppController implements the CRUD actions for App model.
 */
class AppController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            // 'verbs' => [
            //     'class' => VerbFilter::className(),
            //     'actions' => [
            //         'delete' => ['POST'],
            //     ],
            // ],
            'login' => [
                'class'   => \app\filters\LoginFilter::className()
            ],
        ];
    }

    /**
     * Lists all App models.
     * @return mixed
     */
    public function actionIndex()
    {   
        
        // 获取当前请求页的每一行数据
        // $rows = $provider->getModels();
        $searchModel = new AppSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $AppCategory = new MarketCategory;

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'AppCategory' =>$AppCategory,
        ]);
    }

    /**
     * Displays a single App model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new App model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new App();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing App model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing App model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    //更改审核状态
    public function actionUpdateCheckStatus(){
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $post_data = \Yii::$app->request->post('App', 0);
        $id = $post_data['id'];
        $status = $post_data['status'];
        // return ['data' => [$id]];
        $model = $this->findModel($id);
        if ($model->status == $status) {
            return array('ok' => -1, 'message' => '不能修改重复状态');
        }

        $model->status = $status;
        $model->opt_note = $post_data['opt_note'];

        if (!$model->save()) {
            $error = current($model->getErrors());
            return array('ok' => -1, 'message' => '修改状态失败:'.$error[0]);
        }

        return array('ok' => 0, 'message' => '修改成功！');
    }

    //审核弹框
    public function actionCheck($id){
        $model = $this->findModel($id);
        return $this->renderAjax('check_form', [
            'model' => $model,
            'data' => $model::getCheckStatusKeyPair('name'),
        ]);
    }


    /**
     * Finds the App model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return App the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = App::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
